<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "xxxxx");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();

class wechatCallbackapiTest
{
	public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }

    public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

      	//extract post data
		if (!empty($postStr)){
                
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
               
				
				if(!empty( $keyword ))
                {
              	
					$msgType = "news";
					$contentStr = $postObj->Content;
					$title = "按钮";
					$discription = "按钮测试";
					$picUrl = "http://images.sixrevisions.com/2010/12/01-02_baseline_vs_progressive.jpg";
					$url = curPageURL();
					$articles = array();
					$articles[] = makeArticleItem($title, $discription, $picUrl, $url);
					//$articles[] = makeArticle($title, $discription, $picUrl, $url);
					//$articles[] = makeArticle($title, $discription, $picUrl, $url);
					$resultStr = makeArticles($fromUsername, $toUsername, $time, $msgType, $contentStr,$articles); 

					//文本
					// $resultStr = makeText($fromUsername, $toUsername, $time, $msgType, $contentStr); 
					
                	echo $resultStr;
                }else{
                	echo "Input something...";
                }

        }else {
        	echo "";
        	exit;
        }
    }
		
	private function checkSignature()
	{
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}

function makeText($fromUsername, $toUsername, $time, $msgType, $contentStr)
{
	$textTpl = "<xml>
				<ToUserName><![CDATA[%s]]></ToUserName>
				<FromUserName><![CDATA[%s]]></FromUserName>
				<CreateTime>%s</CreateTime>
				<MsgType><![CDATA[%s]]></MsgType>
				<Content><![CDATA[%s]]></Content>
				<FuncFlag>0<FuncFlag>
				</xml>"; 

	return sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
}

function makeArticleItem($title, $discription, $picUrl, $url){
	
	$aTpl = "<item>
			 <Title><![CDATA[%s]]></Title>
			 <Discription><![CDATA[%s]]></Discription>
			 <PicUrl><![CDATA[%s]]></PicUrl>
			 <Url><![CDATA[%s]]></Url>
			 </item>
			 ";

	return sprintf($aTpl, $title, $discription, $picUrl, $url);
	
}

function makeArticles($fromUsername, $toUsername, $time, $msgType, $contentStr, $Articles, $startflag=0){

	$tpl = "<xml>
			 <ToUserName><![CDATA[%s]]></ToUserName>
			 <FromUserName><![CDATA[%s]]></FromUserName>
			 <CreateTime>%s</CreateTime>
			 <MsgType><![CDATA[%s]]></MsgType>
			 <Content><![CDATA[%s]]></Content>
			 <ArticleCount>%d</ArticleCount>
			 <Articles>
			 %s
			 </Articles>
			 <FuncFlag>0</FuncFlag>
			</xml>   ";
	;

	return sprintf($tpl, $fromUsername, $toUsername, $time, $msgType, $contentStr, count($Articles), implode('\n', $Articles), $startflag);
}

function curPageURL() 
{
    $pageURL = 'http';

    if ($_SERVER["HTTPS"] == "on") 
    {
        $pageURL .= "s";
    }
    $pageURL .= "://";

    if ($_SERVER["SERVER_PORT"] != "80") 
    {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } 
    else 
    {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

?>